package com.xs.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xs.common.R;
import com.xs.domain.Comment;
import com.xs.domain.dto.CommentDTO;
import com.xs.mapper.CommentMapper;
import com.xs.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author admin
* @description 针对表【comment(评论)】的数据库操作Service实现
* @createDate 2023-03-02 09:50:16
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
implements CommentService{
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 新增评论
     * @param comment
     * @return
     */
    @Override
    public R insert(Comment comment) {
        Comment comment1 = new Comment();
        if (comment.getType() == 0) {
            comment1.setStaffId(comment.getStaffId());
        } else {
            comment1.setStaffListId(comment.getStaffListId());
        }
        BeanUtils.copyProperties(comment, comment1);
        int a = commentMapper.insert(comment1);
        if (a > 0) {
            return R.success("评论成功");
        } else {
            return R.error(400, "评论失败");
        }

    }

    /**
     * 修改评论
     * @param comment
     * @return
     */
    @Override
    public R updateComment(Comment comment) {
        int c = commentMapper.updateById(comment);
        if (c > 0) {
            return R.success("修改成功");
        } else {
            return R.error(400, "修改失败");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public R deleteComment(Integer id) {
        if (commentMapper.deleteById(id) > 0) {
            return R.success("删除成功");
        } else {
            return R.error(400, "删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public R deleteBathIds(List<Integer> ids) {
        int i = commentMapper.deleteBatchIds(ids);
        if (i < 0) {
            return R.error(400, "批量删除失败");
        }
        return R.success("批量删除成功");
    }


    /**
     * 查询所有评论
     * @return
     */
    @Override
    public R allComment() {
        if (commentMapper.selectList(null) != null) {
            return R.success("查找成功");
        } else {
            return R.error(400, "查找失败");
        }

    }

    /**
     * 查询某个乐谱下的所有评论
     * @param staffId
     * @return
     */
    @Override
    public R commentOfStaffId(Integer staffId) {
        List<CommentDTO> comments = commentMapper.commentOfStaffId(staffId);
        if (comments != null) {
            return R.success("查找成功", comments);
        } else {
            return R.error(400, "查找失败");
        }
    }

    /**
     * 查询某个乐谱集下的所有评论
     * @param staffListId
     * @return
     */
    @Override
    public R commentOfStaffListId(Integer staffListId) {
        List<CommentDTO> comments = commentMapper.commentOfStaffListId(staffListId);
        if (comments != null) {
            return R.success("查找成功", comments);
        } else {
            return R.error(400, "查找失败");
        }
    }

}
